package com.ciwong.epaper.modules.me.ui;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.dictation.ui.DictationRedoAndResultActivity;
import com.ciwong.epaper.modules.epaper.adapter.DubVideoListAdapter;
import com.ciwong.epaper.modules.epaper.bean.ModuleInfo;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.modules.me.widget.SpaceItemDecoration;
import com.ciwong.epaper.modules.msg.bean.WorkContent;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.BaseQuickOnItemClickListener;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 趣味配音列表
 */

public class DubVideoListActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {
    private RecyclerView mRvBookDesk;
    private DubVideoListAdapter mDubVideoListAdapter;
    private List<WorkContent> mWorkContentList = new ArrayList<>();
    private SwipeToLoadLayout mSwipeToLoadLayout;
    private int gradeId;
    private int mPageIndex = 1;
    private int mPageSize = 10;
    private String title;
    private boolean mIsRefesh;
    private LinearLayout list_nodata;
    private ViewGroup listviewgroup;
    private Button btn_retry;
    private RelativeLayout rl_search_container;

    @Override
    protected int setView() {
        return R.layout.fragment_book_desk;
    }

    @Override
    protected void findViews() {
        mRvBookDesk = findViewById(R.id.swipe_target);
        mSwipeToLoadLayout = findViewById(R.id.swipe_toLoad_layout);
        list_nodata = findViewById(R.id.ll_no_net);
        listviewgroup = findViewById(R.id.listviewgroup);
        btn_retry = findViewById(R.id.btn_retry);
        rl_search_container = findViewById(R.id.rl_search_container);
    }

    @Override
    protected void initEvent() {
        title = "趣配音";
        setTitleText(title);
        showRightBtn();
        setRightBtnText("我的作品");
        setRightBtnListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View v) {
                jumpToStudyRecordH5();
            }
        });
//        setRightBtnText("配音记录");
        mSwipeToLoadLayout.setOnRefreshListener(this);
        mSwipeToLoadLayout.setOnLoadMoreListener(this);
        rl_search_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MeJumpManager.jumpToSearchDubActivity(R.string.go_back, DubVideoListActivity.this);
            }
        });
        btn_retry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData();
            }
        });
//        setRightBtnListener(new ViewOnClickListener() {
//            @Override
//            public void avertRepeatOnClick(View v) {
//                //点击更多配音记录
//                MeJumpManager.jumpToDubVideoRecordListActivity(R.string.go_back, DubVideoListActivity.this);
//            }
//        });
    }

    private void jumpToStudyRecordH5() {
        try {
            //直接跳转到新的学习记录页面
            StringBuffer sb = new StringBuffer();
            String title;
            try {
                title = java.net.URLEncoder.encode("我的作品", "UTF-8");
            } catch (Exception e) {
                title = "";
            }
            title = title.replace("+", "%20");
            sb.append("&title=");
            sb.append(title);
            MeJumpManager.jumpToStudyRecordH5Activity(ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT, DubVideoListActivity.this, R.string.go_back, -1, "", "我的作品", 0);

        } catch (Exception e) {
            e.getStackTrace();
        }
    }



    @Override
    protected void init() {
        rl_search_container.setVisibility(View.VISIBLE);
        mRvBookDesk.setLayoutManager(new GridLayoutManager(DubVideoListActivity.this, 2));
        mRvBookDesk.addItemDecoration(new SpaceItemDecoration(DubVideoListActivity.this, 10, 10, 10));
        mDubVideoListAdapter = new DubVideoListAdapter(R.layout.item_view_dub_video, mWorkContentList);
        mRvBookDesk.setAdapter(mDubVideoListAdapter);

        mDubVideoListAdapter.setOnItemClickListener(new BaseQuickOnItemClickListener() {
            @Override
            public void avertRepeatOnClick(BaseQuickAdapter adapter, View view, int position) {
                try {
                    WorkContent workContent = mWorkContentList.get(position);
                    if (null != workContent) {
                        MeJumpManager.jumpToDubVideoDetailActivity(EpaperConstant.REQUEST_CODE_GET_VIDEO_MASTER_STATE, R.string.go_back,
                                DubVideoListActivity.this, "", workContent, "0", 0, false);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        if (EApplication.getInstance().getClazz() != null) {
            gradeId = EApplication.getInstance().getClazz().getGradeId();
        }
    }

    @Override
    protected void loadData() {
        mIsRefesh = true;
        mPageIndex = 1;
        if (!NetworkUtils.isOnline()) {
            list_nodata.setVisibility(View.VISIBLE);
            listviewgroup.setVisibility(View.GONE);
        } else {
            list_nodata.setVisibility(View.GONE);
            listviewgroup.setVisibility(View.VISIBLE);
            getBookBooksFromNet(mPageIndex);
        }

    }

    public void getBookBooksFromNet(int pageIndex) {
        showProgressLoading();
        EpaperDao.getInstance().getDubbingVideoList(gradeId, pageIndex, mPageSize, new BaseExtCallBack(DubVideoListActivity.this) {
            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                dismissProgress();
                mSwipeToLoadLayout.setRefreshing(false);
                mSwipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void failed(Object data) {
                super.failed(data);
                dismissProgress();
                mSwipeToLoadLayout.setRefreshing(false);
                mSwipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void success(Object data) {
                dismissProgress();
                mSwipeToLoadLayout.setRefreshing(false);
                mSwipeToLoadLayout.setLoadingMore(false);
                try {
                    if (data != null && data instanceof ArrayList) {
                        List<WorkContent> workContentList = (ArrayList<WorkContent>) data;
                        if (mIsRefesh) {
                            if (workContentList != null && workContentList.size() >= 0) {
                                mWorkContentList = workContentList;
                                mDubVideoListAdapter.setNewData(mWorkContentList);
                            }
                        } else {
                            if (workContentList != null && workContentList.size() >= 0) {
                                mDubVideoListAdapter.addData(workContentList);
                            }
                        }

                    }
                } catch (Exception e) {
                    e.getStackTrace();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        mIsRefesh = true;
        mPageIndex = 1;
        getBookBooksFromNet(mPageIndex);
    }

    @Override
    public void onLoadMore() {
        mIsRefesh = false;
        mPageIndex++;
        getBookBooksFromNet(mPageIndex);
    }

    private void showProgressLoading() {
        showMiddleProgressBar(title);
    }

    private void dismissProgress() {
        hideMiddleProgressBar();
    }
}

